package com.example.service;

import com.example.task.TimeTask;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;

@Service // 标记为 Spring 服务组件，让 Spring 扫描并管理
public class TaskService {

    private final ExecutorService threadPool;

    // 构造器注入线程池（由 ThreadPoolConfig 配置）
    public TaskService(ExecutorService threadPool) {
        this.threadPool = threadPool;
    }

    // 提交“每秒输出时间”任务
    public void startPrintTimeTask(int durationSeconds) {
        threadPool.submit(() -> new TimeTask().printTimeEverySecond(durationSeconds));
    }

    // 提交“倒计时”任务
    public void startCountdownTask(int seconds) {
        threadPool.submit(() -> new TimeTask().countdown(seconds));
    }

    // 关闭线程池
    public void shutdownThreadPool() {
        threadPool.shutdown();
    }
}